﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Studio77.GuessTheWordsForWP7.Entity;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Resources;

namespace Studio77.GuessTheWordsForWP7.Util
{

    /// <summary>
    /// 地图加载器
    /// </summary>
    public class WordMapLoader
    {
        public static MapInfo LoadMap(int index)
        {
            MapInfo mapInfo = new MapInfo();
            String fileName = "stage" + index + ".map";
            try
            {
                //StreamResourceInfo reader = Application.GetResourceStream(new Uri("/GuessTheWordsForWP7;component/Stages/" + fileName, UriKind.Relative));
                StreamResourceInfo reader = Application.GetResourceStream(new Uri("Stages/" + fileName, UriKind.Relative));
                using (StreamReader sr = new StreamReader(reader.Stream, Encoding.UTF8))
                {

                    // 读取wordCount
                    String c = sr.ReadLine().Trim();
                    String count = GetBase64String(c);
                    int wordCount = int.Parse(count);
                    mapInfo.wordsCount = wordCount;

                    // 读取所有词条
                    List<WordInfo> words = new List<WordInfo>();
                    for (int i = 0; i < wordCount; i++)
                    {
                        String wordComponentTemp = sr.ReadLine();
                        String wordComponent = GetBase64String(wordComponentTemp);
                        String[] compoents = Regex.Split(wordComponent, "--", RegexOptions.IgnoreCase);
                        WordInfo info = new WordInfo();
                        info.pinyin = compoents[0];
                        info.content = compoents[1];
                        info.tip = compoents[2];
                        words.Add(info);
                    }
                    mapInfo.wordList = words;

                    // 读取字符位置
                    List<CharacterLocationInfo> locations = new List<CharacterLocationInfo>();
                    String location64 = sr.ReadLine();
                    int lineIndex = 0;

                    while (location64 != null && location64 != "")
                    {

                        CharacterLocationInfo locationInfo = new CharacterLocationInfo();

                        String location = GetBase64String(location64);

                        String[] locationComponent = Regex.Split(location, ",", RegexOptions.IgnoreCase); ;
                        locationInfo.row = int.Parse(locationComponent[0]);
                        locationInfo.column = int.Parse(locationComponent[1]);

                        if (locationComponent[2] != null
                                && locationComponent[2] != ("N"))
                        {
                            locationInfo.orientationWordIndex = int.Parse(locationComponent[2]);
                            locationInfo.orientationCharIndex = int.Parse(locationComponent[3]);
                        }
                        if (locationComponent[4] != null
                                && locationComponent[4] != ("N"))
                        {
                            locationInfo.verticalWordIndex = int.Parse(locationComponent[4]);
                            locationInfo.verticalCharIndex = int.Parse(locationComponent[5]);
                        }
                        locations.Add(locationInfo);

                        location64 = sr.ReadLine();
                        lineIndex++;
                    }

                    mapInfo.charLocationList = locations;

                }
            }
            catch (Exception ex)
            {
                
            }

            return mapInfo;
        }

        /// <summary>
        /// 获取base64代码
        /// </summary>
        /// <param name="base64"></param>
        /// <returns></returns>
        static string GetBase64String(string base64)
        {
            byte[] bytes = Convert.FromBase64String(base64);
            return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
        }

    }
}
